package com.youi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.ActivitygoodsDao;
import com.youi.entity.Activity;
import com.youi.entity.Activitygoods;
import com.youi.entity.Goods;
import com.youi.entity.Goodssize;
import com.youi.service.ActivityService;
import com.youi.service.ActivitygoodsService;
import com.youi.service.GoodsService;
import com.youi.service.GoodssizeService;

@Service
public class ActivitygoodsServiceImpl extends BaseServiceImpl<Activitygoods> implements ActivitygoodsService{
		
		private ActivitygoodsDao activitygoodsDao;
		@Autowired
		private ActivityService activityService;
		@Autowired
		private GoodsService goodsService;
		@Autowired
		private GoodssizeService goodssizeService;
		@Autowired
		public void setfaultRecordDao(ActivitygoodsDao activitygoodsDao){
			this.activitygoodsDao=activitygoodsDao;
			super.setBaseDAO(activitygoodsDao);
		}

		@Override
		public void getActivitygoodsMsg(List<Activitygoods> activitygoodsList) {
			// TODO Auto-generated method stub
			if (activitygoodsList!=null&&activitygoodsList.size()>0) {
				for (Activitygoods activitygoods : activitygoodsList) {
					Activity activity=activityService.get(activitygoods.getActivityId());
					if (activity!=null) {
						activitygoods.setActivity(activity);
					}
					Map<String, Object> params=new HashMap<String, Object>();
					params.put(Common.DELSTATUS, DelStatus.NODEL);
					params.put(Common.ID, activitygoods.getGoodsId());
					Goods goods=goodsService.getByParam(params);
					Integer realSaleCnt=0; 
					Integer allStockNum=0;
					Double salePrice=0.00;
					Double marketPrice=0.00;
					if (goods!=null) {
						realSaleCnt=goods.getRealSaleCnt();
						params=new HashMap<String, Object>();
						params.put(Common.DELSTATUS, DelStatus.NODEL);
						params.put("goodsId", activitygoods.getGoodsId());
						List<Goodssize> goodssizeList=goodssizeService.getByParams(params);					
						if (goodssizeList!=null&&goodssizeList.size()>0) {
							for (Goodssize goodssize : goodssizeList) {
								allStockNum+=goodssize.getStockNum();
							}
							Goodssize goodssize=goodssizeList.get(0);
							salePrice=goodssize.getPrice();
							marketPrice=goods.getEcPrice();
						}
						activitygoods.setRealSaleCnt(realSaleCnt);
						activitygoods.setAllStockNum(allStockNum);
						activitygoods.setSalePrice(salePrice);
						activitygoods.setMarketPrice(marketPrice);
					}else {
						activitygoods.setDelStatus(DelStatus.DEL);
						activitygoodsDao.saveOrUpdate(activitygoods);
					}										
				}
			}
		}

		@Override
		public void getActivitygoodsMsg(Activitygoods activitygoods) {
			// TODO Auto-generated method stub
			if (activitygoods!=null) {
				Activity activity=activityService.get(activitygoods.getActivityId());
				if (activity!=null) {
					activitygoods.setActivity(activity);
				}
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put(Common.ID, activitygoods.getGoodsId());
				Goods goods=goodsService.getByParam(params);
				Integer realSaleCnt=0; 
				Integer allStockNum=0;
				Double salePrice=0.00;
				Double marketPrice=0.00;
				if (goods!=null) {
					realSaleCnt=goods.getRealSaleCnt();
					List<Goodssize> goodssizeList=goodssizeService.getByParams(params);					
					if (goodssizeList!=null&&goodssizeList.size()>0) {
						for (Goodssize goodssize : goodssizeList) {
							allStockNum+=goodssize.getStockNum();
						}
						Goodssize goodssize=goodssizeList.get(0);
						salePrice=goodssize.getPrice();
						marketPrice=goods.getEcPrice();
					}
				}				
				activitygoods.setRealSaleCnt(realSaleCnt);
				activitygoods.setAllStockNum(allStockNum);
				activitygoods.setSalePrice(salePrice);
				activitygoods.setMarketPrice(marketPrice);
			}
		}
		
		@Override
		public List<Long> getGoodsIdByPms(String pms) {
			// TODO Auto-generated method stub
			if (pms!=null&&pms.trim().length()>0) {
				List<Long> goodsIdList=null;
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				Map<String, Object> searchParams=new HashMap<String, Object>();
				searchParams.put("name", pms);
				searchParams.put("goodsNo", pms);
				List<Goods> goodsList=goodsService.getByParams(params, searchParams, null);
				if (goodsList!=null&&goodsList.size()>0) {
					goodsIdList=new ArrayList<Long>();
					for (Goods goods : goodsList) {
						goodsIdList.add(goods.getId());
					}
					return goodsIdList;
				}
			}			
			return null;
		}
}